<template>
    <section class="chart-container">
        <el-row>
            <el-col :span="12">
                <div id="chartColumn" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartBar" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartLine" style="width:100%; height:400px;"></div>
            </el-col>
            <el-col :span="12">
                <div id="chartPie" style="width:100%; height:400px;"></div>
            </el-col>
        </el-row>
    </section>
</template>

<script>
import echarts from 'echarts'

export default {
    data() {
        return {
            chartColumn: null,
            column: [],
            stars: [],
            chartBar: null,
            chartLine: null,
            chartPie: null,
        }
    },

    methods: {
        drawColumnChart() {
            this.$http.get("/article/orderByStar").then(res => {
                this.chartColumn = echarts.init(document.getElementById('chartColumn'));
                var name = res.data.map(obj => {
                    return obj.articleName
                })
                var stars = res.data.map(obj => {
                    return obj.stars
                })
                this.chartColumn.setOption({
                    title: { text: 'Column Chart' },
                    tooltip: {
                        type: 'category',
                    },
                    xAxis: {
                        data: name,
                        axisLabel: {
                            interval: 0,
                            formatter: function (value) {
                                let len = value.length;
                                let length = 7; //控制一行显示个数
                                let num = Math.ceil(len / length);//循环次数
                                if (num > 1) {
                                    let str = '';
                                    for (let i = 0; i < num; i++) {
                                        str += value.substring(i * length, (i + 1) * length) + '\n';
                                    }
                                    return str;
                                } else {
                                    return value;
                                }
                            }
                        }

                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [{
                        name: '点赞数',
                        type: 'bar',
                        data: stars
                    }],

                });
            }).catch(res => {
                this.$message.error("请求超时请重试")
            })
        },
        drawBarChart() {
            this.$http.get("/article/orderByStar").then(res => {
            this.chartBar = echarts.init(document.getElementById('chartBar'));
            var name = res.data.map(obj => {
                    return obj.articleName
                })
            var stars = res.data.map(obj => {
                    return obj.stars
                })
            var read = res.data.map(obj=>{
                return obj.readCount
            })
            this.chartBar.setOption({
                title: {
                    text: 'Bar Chart',
                    subtext: '数据来自网络'
                },
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                legend: {
                    data: ['star', 'read']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'value',
                    // boundaryGap: [0, 0.01]
                },
                yAxis: {
                    type: 'category',
                    data: name
                },
                series: [
                    {
                        name: 'star',
                        type: 'bar',
                        data: stars
                    },
                    {
                        name: 'read',
                        type: 'bar',
                        data: read
                    }
                ]
            });
        })
        },
        drawLineChart() {
            this.chartLine = echarts.init(document.getElementById('chartLine'));
            this.chartLine.setOption({
                title: {
                    text: 'Line Chart'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['邮件营销', '联盟广告', '搜索引擎']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '邮件营销',
                        type: 'line',
                        stack: '总量',
                        data: [120, 132, 101, 134, 90, 230, 210]
                    },
                    {
                        name: '联盟广告',
                        type: 'line',
                        stack: '总量',
                        data: [220, 182, 191, 234, 290, 330, 310]
                    },
                    {
                        name: '搜索引擎',
                        type: 'line',
                        stack: '总量',
                        data: [820, 932, 901, 934, 1290, 1330, 1320]
                    }
                ]
            });
        },
        drawPieChart() {
            this.$http.get("/article/orderByNum").then(res => {
            this.chartPie = echarts.init(document.getElementById('chartPie'));
            // var name = res.data.map(obj => {
            //         return obj.aticleTypeName
            //     })
            // var num = res.data.map(obj => {
            //         return obj.num
            //     })
            var dataForType = []
            res.data.forEach(element => {
                let data ={value:element.num,name:element.articleTypeName}
                dataForType.push(data)
            });    
            // var read = res.data.map(obj=>{
            //     return obj.readCount
            // })
            this.chartPie.setOption({
                title: {
                    text: 'Pie Chart',
                    subtext: '纯属虚构',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎']
                },
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: dataForType
                        ,
                        /* [
                            { value: 335, name: '直接访问' },
                            { value: 310, name: '邮件营销' },
                            { value: 234, name: '联盟广告' },
                            { value: 135, name: '视频广告' },
                            { value: 1548, name: '搜索引擎' }
                        ], */
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            });
        })
        },
        drawCharts() {
            this.drawColumnChart()
            this.drawBarChart()
            this.drawLineChart()
            this.drawPieChart()
        },
        getColumn() {
            this.$http.get("/article/orderByStar").then(res => {
                var arr = []
                var likeNum = []
                res.data.forEach(element => {
                    arr.push(element.articleName)
                    likeNum.push(element.stars)
                });
                this.column = arr
                this.stars = likeNum
                console.log("--------" + this.column);
                console.log("+++++++++" + this.stars);
            }).catch(res => {
                this.$message.error("请求超时请重试")
            })
        }
    },

    mounted: function () {
        this.getColumn();
        this.drawCharts()
    },
    updated: function () {
        this.drawCharts()
    }
}
</script>

<style scoped>
.chart-container {
    width: 100%;
    float: left;
}

/*.chart div {
        height: 400px;
        float: left;
    }*/

.el-col {
    padding: 30px 20px;
}
</style>
